﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Model = RBS.Core.Model;
using BLL = RBS.Core.BLL;
using RBS.Web.Hotel.Models;

namespace RBS.Web.Hotel.Service
{
    /// <summary>
    /// CookBook服务
    /// </summary>
    public class CookBookService
    {
        private BLL.SYS_DictInfo _sysDictInfo_bll = null;

        private CookBookService() {
            _sysDictInfo_bll = new BLL.SYS_DictInfo();
        }

        public static CookBookService Instance {
            get {
                return new CookBookService();
            }
        }

        #region 获取菜系


        public CookBookCaiXiViewModel GetCookBookCaiXi(String id)
        {

            #region 测试数据

            IList<CookBookCaiXiViewModel> list = new List<CookBookCaiXiViewModel>(){
                new CookBookCaiXiViewModel(){
                    ID=1,
                    Name="tree1",
                    Childs=new List<CookBookCaiXiViewModel>(){
                        new CookBookCaiXiViewModel(101,"tree_101","tree101"),
                        new CookBookCaiXiViewModel(102,"tree_102","tree102")
                    }
                },
                new CookBookCaiXiViewModel(){
                    ID=2,
                    Name="tree2",
                    Childs=new List<CookBookCaiXiViewModel>(){
                        new CookBookCaiXiViewModel(201,"tree_201","tree201"),
                        new CookBookCaiXiViewModel(202,"tree_202","tree202")
                    }
                },
                new CookBookCaiXiViewModel(){
                    ID=3,
                    Name="tree3"
                },
                new CookBookCaiXiViewModel(){
                    ID=4,
                    Name="tree4"
                }
            };

            #endregion

            Int32 int_id = 0;
            if (Int32.TryParse(id, out int_id)) {
                var model = _sysDictInfo_bll.GetModel(int_id);

                if (model == null) return null;
                return new CookBookCaiXiViewModel()
                {
                    ID = model.DictID,
                    Code = model.DictCode,
                    Name = model.DictName
                };
            }

            return null;            
        }

        /// <summary>
        /// 获取菜系列表
        /// </summary>
        /// <returns></returns>
        public IList<CookBookCaiXiViewModel> GetCookBookCaiXiList()
        {
            IList<CookBookCaiXiViewModel> list = null;

            #region 测试数据

            list = new List<CookBookCaiXiViewModel>(){
                new CookBookCaiXiViewModel(){
                    ID=1,
                    Name="tree1",
                    Childs=new List<CookBookCaiXiViewModel>(){
                        new CookBookCaiXiViewModel(101,"tree_101","tree101"),
                        new CookBookCaiXiViewModel(102,"tree_102","tree102")
                    }
                },
                new CookBookCaiXiViewModel(){
                    ID=2,
                    Name="tree2",
                    Childs=new List<CookBookCaiXiViewModel>(){
                        new CookBookCaiXiViewModel(201,"tree_201","tree201"),
                        new CookBookCaiXiViewModel(202,"tree_202","tree202")
                    }
                },
                new CookBookCaiXiViewModel(){
                    ID=3,
                    Name="tree3"
                },
                new CookBookCaiXiViewModel(){
                    ID=4,
                    Name="tree4"
                }
            };

            #endregion

            #region 查询数据

            list = new List<CookBookCaiXiViewModel>();

            //获取根节点
            var dictList = getChildDictList(1);     //换成菜系字典的root id
            foreach (var dict in dictList) {
                CookBookCaiXiViewModel model = new CookBookCaiXiViewModel()
                {
                    ID = dict.DictID,
                    Code = dict.DictCode,
                    Name = dict.DictName,
                    Childs = getChildCaiXiList(dict.DictID)
                };
                list.Add(model);
            }

            #endregion

            return list;
        }

        private IList<CookBookCaiXiViewModel> getChildCaiXiList(Int32 parentId) {
            IList<CookBookCaiXiViewModel> list = null;

            var childList = getChildDictList(parentId);
            if (childList != null && childList.Any())
            {
                list = new List<CookBookCaiXiViewModel>();
                foreach (var dict in childList) {
                    CookBookCaiXiViewModel model = new CookBookCaiXiViewModel()
                    {
                        ID = dict.DictID,
                        Code = dict.DictCode,
                        Name = dict.DictName,
                        Childs = getChildCaiXiList(dict.DictID)
                    };
                    list.Add(model);
                }
            }

            return list;
        }

        #endregion

        /// <summary>
        /// 获取种类列表(分类)
        /// </summary>
        /// <returns></returns>
        public IList<CookBookSpeciesViewModel> GetCookBookSpeciesList() {
            IList<CookBookSpeciesViewModel> list = null;

            return list;
        }

        #region 菜谱单位

        /// <summary>
        /// 返回菜谱单位model
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CookBookUnitViewModel GetCookBookUnit(String id) {
            CookBookUnitViewModel model = null;

            Int32 int_id = 0;
            if (Int32.TryParse(id, out int_id)) {
                var dict = _sysDictInfo_bll.GetModel(int_id);
                if (dict == null) return null;
                return new CookBookUnitViewModel()
                {
                    ID = dict.DictID,
                    Name = dict.DictName,
                    Code = dict.DictCode
                };
            }

            return model;
        }

        /// <summary>
        /// 获取菜谱单位列表
        /// </summary>
        /// <returns></returns>
        public IList<CookBookUnitViewModel> GetCookBookUnitList() {
            IList<CookBookUnitViewModel> list = null;

            #region 测试数据

          /*  list = new List<CookBookUnitViewModel>(){
                new CookBookUnitViewModel(1001,"CBUnit_1001","测试1"),
                new CookBookUnitViewModel(1002,"CBUnit_1002","测试2"),
                new CookBookUnitViewModel(1003,"CBUnit_1003","测试3"),
                new CookBookUnitViewModel(1004,"CBUnit_1004","测试4")
            };*/

            #endregion

            var dictList = getChildDictList(40);         //换成菜谱单位字典的root id
            if (dictList != null && dictList.Any()) {
                list = new List<CookBookUnitViewModel>();
                foreach (var dict in dictList) {
                    list.Add(new CookBookUnitViewModel()
                    {
                        ID = dict.DictID,
                        Code = dict.DictCode,
                        Name = dict.DictName
                    });
                }
            }

            return list;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 获取子字典
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        private IList<Model.SYS_DictInfo> getChildDictList(Int32 parentId)
        {
            IList<Model.SYS_DictInfo> list = null;
            list = _sysDictInfo_bll.GetModelList(String.Format(" ParentID = '{0}' Order by Sequence asc ", parentId));

            return list;
        }

        #endregion


    }
}